package com.new_wear.project.system.cst_user.controller;

import java.util.Date;
import java.util.List;

import com.new_wear.project.system.info.domain.CstAdInfo;
import com.new_wear.project.system.log.domain.CstUserIntegralLog;
import com.new_wear.project.system.log.service.ICstUserIntegralLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.cst_user.domain.CstUser;
import com.new_wear.project.system.cst_user.service.ICstUserService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 用户 - 志愿者信息Controller
 * 
 * @author new_wear
 * @date 2019-12-30
 */
@Controller
@RequestMapping("/system/cst_user")
public class CstUserController extends BaseController
{
    private String prefix = "system/cst_user";

    @Autowired
    private ICstUserService cstUserService;

    @Autowired
    private ICstUserIntegralLogService cstUserIntegralLogService;

    @RequiresPermissions("system:cst_user:view")
    @GetMapping()
    public String cst_user()
    {
        return prefix + "/cst_user";
    }

    /**
     * 查询用户 - 志愿者信息列表
     */
    @RequiresPermissions("system:cst_user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstUser cstUser)
    {
        startPage();
        List<CstUser> list = cstUserService.selectCstUserList(cstUser);
        Long integral=new Long(0l);

        for (CstUser cstUserList:list) {
            //根据用户id 查询 该用户的所有积分记录
            CstUserIntegralLog cstUserIntegralLog = new CstUserIntegralLog();
            cstUserIntegralLog.setUid(cstUserList.getId());
            List<CstUserIntegralLog> cstUserIntegralLogs = cstUserIntegralLogService.selectCstUserIntegralLogList(cstUserIntegralLog);

            //循环改积分记录 并相加 所有积分 得出总和显示
            for (CstUserIntegralLog log:cstUserIntegralLogs ) {
                Long scores = log.getScores();
                integral = scores +integral;
            }
            cstUserList.setIntegral(integral);
        }
        return getDataTable(list);
    }

    /**
     * 导出用户 - 志愿者信息列表
     */
    @RequiresPermissions("system:cst_user:export")
    @Log(title = "用户 - 志愿者信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstUser cstUser)
    {
        List<CstUser> list = cstUserService.selectCstUserList(cstUser);
        ExcelUtil<CstUser> util = new ExcelUtil<CstUser>(CstUser.class);
        return util.exportExcel(list, "cst_user");
    }

    /**
     * 新增用户 - 志愿者信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户 - 志愿者信息
     */
    @RequiresPermissions("system:cst_user:add")
    @Log(title = "用户 - 志愿者信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstUser cstUser)
    {
        return toAjax(cstUserService.insertCstUser(cstUser));
    }

    /**
     * 修改用户 - 志愿者信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CstUser cstUser = cstUserService.selectCstUserById(id);
        mmap.put("cstUser", cstUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户 - 志愿者信息
     */
    @RequiresPermissions("system:cst_user:edit")
    @Log(title = "用户 - 志愿者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstUser cstUser)
    {
        return toAjax(cstUserService.updateCstUser(cstUser));
    }

    /**
     * 删除用户 - 志愿者信息
     */
    @RequiresPermissions("system:cst_user:remove")
    @Log(title = "用户 - 志愿者信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstUserService.deleteCstUserByIds(ids));
    }



    @GetMapping("/Integral/{id}")
    public String Integral(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/Integral";
    }

    @RequestMapping("/IntegralLog/{id}")
    @ResponseBody
    public TableDataInfo IntegralLog(@PathVariable("id")  Long id){
        CstUserIntegralLog cstUserIntegral = new CstUserIntegralLog();
        cstUserIntegral.setUid(id);
        List<CstUserIntegralLog> integralLogList = cstUserIntegralLogService.selectCstUserIntegralLogList(cstUserIntegral);
        return getDataTable(integralLogList);
    }

    /*调整*/
    @GetMapping("/stment/{id}")
    public String adjustment(@PathVariable("id")  Long id ,ModelMap map){
        map.put("id",id);
        return prefix + "/adjustment";
    }

    @PostMapping("/adju")
    @ResponseBody
    public AjaxResult adju(CstUserIntegralLog cstUserIntegralLog)
    {
        cstUserIntegralLog.setEventCode("系统调整");
        cstUserIntegralLog.setEventNote("SysAdjust");
        cstUserIntegralLog.setCreateAt(new Date());
        return toAjax(cstUserIntegralLogService.insertCstUserIntegralLog(cstUserIntegralLog));
    }
}
